package com.xpoint.backend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xpoint.backend.common.R;
import com.xpoint.backend.entity.Employee;
import com.xpoint.backend.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/employee")
//员工管理
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
//    登录到管理系统
    public R<Employee> login(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.login(employee, request);
    }

    @PostMapping("/logout")
//    退出登录
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    @PostMapping
//    添加员工
    public R<String> savenEmp(@RequestBody Employee employee, HttpServletRequest request) {
        return employeeService.sevaEmp(employee, request);
    }

    @GetMapping("/page")
//    分页查询
    public R<Page> page(Long page, Long pageSize, String name) {
        return employeeService.findPage(page, pageSize, name);
    }

    @GetMapping("/{id}")
//    根据id查询
    public R<Employee> findById(@PathVariable Long id) {
        return employeeService.findByid(id);
    }

    @PutMapping
//    禁用或者启用员工状态
    public R<String> enableOrDisable(@RequestBody Employee employee,HttpServletRequest request) {
        return employeeService.updateEmp(employee,request);
    }

}